Fix the opposite size measurement in GtkConstraintLayout
authorEmmanuele Bassi <ebassi@gnome.org>
Thu, 27 Jun 2019 16:34:29 +0000 (17:34 +0100)
committerEmmanuele Bassi <ebassi@gnome.org>
Sun, 30 Jun 2019 22:42:44 +0000 (23:42 +0100)
We cannot use the given "for size" when querying our children, because
the constraint layout has no idea about the opposite size of its
children until the layout is complete.

Additionally, we should only suggest an opposite size for the layout if
we have one, instead of suggesting a weak zero size.

gtk/gtkconstraintlayout.c

index a111dbe591de288c4c24b350b24b4bc4f0585ee1..86cdd2840fb17f1be725cafb4341cbdf9925c60c 100644 (file)
@@ -693,57 +693,47 @@ gtk_constraint_layout_measure (GtkLayoutManager *manager,
 
       child_info = GTK_CONSTRAINT_LAYOUT_CHILD (gtk_layout_manager_get_layout_child (manager, child));
 
-      gtk_widget_measure (child, orientation, for_size,
+      gtk_widget_measure (child, orientation, -1,
                           &min_size, &nat_size,
                           NULL, NULL);
 
-      switch (orientation)
-        {
-        case GTK_ORIENTATION_HORIZONTAL:
-          width_var = get_child_attribute (child_info, solver, child,
-                                           GTK_CONSTRAINT_ATTRIBUTE_WIDTH);
-
-          constraint =
-            gtk_constraint_solver_add_constraint (solver,
-                                                  width_var,
-                                                  GTK_CONSTRAINT_RELATION_GE,
-                                                  gtk_constraint_expression_new (min_size),
-                                                  GTK_CONSTRAINT_WEIGHT_REQUIRED);
-          g_ptr_array_add (size_constraints, constraint);
-
-          constraint =
-            gtk_constraint_solver_add_constraint (solver,
-                                                  width_var,
-                                                  GTK_CONSTRAINT_RELATION_EQ,
-                                                  gtk_constraint_expression_new (nat_size),
-                                                  GTK_CONSTRAINT_WEIGHT_MEDIUM);
-          g_ptr_array_add (size_constraints, constraint);
-          break;
-
-        case GTK_ORIENTATION_VERTICAL:
-          height_var = get_child_attribute (child_info, solver, child,
-                                            GTK_CONSTRAINT_ATTRIBUTE_HEIGHT);
-
-          constraint =
-            gtk_constraint_solver_add_constraint (solver,
-                                                  height_var,
-                                                  GTK_CONSTRAINT_RELATION_GE,
-                                                  gtk_constraint_expression_new (min_size),
-                                                  GTK_CONSTRAINT_WEIGHT_REQUIRED);
-          g_ptr_array_add (size_constraints, constraint);
-
-          constraint =
-            gtk_constraint_solver_add_constraint (solver,
-                                                  height_var,
-                                                  GTK_CONSTRAINT_RELATION_EQ,
-                                                  gtk_constraint_expression_new (nat_size),
-                                                  GTK_CONSTRAINT_WEIGHT_MEDIUM);
-          g_ptr_array_add (size_constraints, constraint);
-          break;
-
-        default:
-          break;
-        }
+      width_var = get_child_attribute (child_info, solver, child,
+                                       GTK_CONSTRAINT_ATTRIBUTE_WIDTH);
+
+      constraint =
+        gtk_constraint_solver_add_constraint (solver,
+                                              width_var,
+                                              GTK_CONSTRAINT_RELATION_GE,
+                                              gtk_constraint_expression_new (min_size),
+                                              GTK_CONSTRAINT_WEIGHT_REQUIRED);
+      g_ptr_array_add (size_constraints, constraint);
+
+      constraint =
+        gtk_constraint_solver_add_constraint (solver,
+                                              width_var,
+                                              GTK_CONSTRAINT_RELATION_EQ,
+                                              gtk_constraint_expression_new (nat_size),
+                                              GTK_CONSTRAINT_WEIGHT_MEDIUM);
+      g_ptr_array_add (size_constraints, constraint);
+
+      height_var = get_child_attribute (child_info, solver, child,
+                                        GTK_CONSTRAINT_ATTRIBUTE_HEIGHT);
+
+      constraint =
+        gtk_constraint_solver_add_constraint (solver,
+                                              height_var,
+                                              GTK_CONSTRAINT_RELATION_GE,
+                                              gtk_constraint_expression_new (min_size),
+                                              GTK_CONSTRAINT_WEIGHT_REQUIRED);
+      g_ptr_array_add (size_constraints, constraint);
+
+      constraint =
+        gtk_constraint_solver_add_constraint (solver,
+                                              height_var,
+                                              GTK_CONSTRAINT_RELATION_EQ,
+                                              gtk_constraint_expression_new (nat_size),
+                                              GTK_CONSTRAINT_WEIGHT_MEDIUM);
+      g_ptr_array_add (size_constraints, constraint);
     }
 
   switch (orientation)
@@ -769,15 +759,22 @@ gtk_constraint_layout_measure (GtkLayoutManager *manager,
    * natural state of the system. Once we get the value out, we can
    * remove these constraints
    */
-  gtk_constraint_solver_add_edit_variable (solver, size, GTK_CONSTRAINT_WEIGHT_WEAK + 1);
-  gtk_constraint_solver_add_edit_variable (solver, opposite_size, GTK_CONSTRAINT_WEIGHT_WEAK + 2);
-
-  gtk_constraint_solver_begin_edit (solver);
+  if (for_size > 0)
+    {
+      gtk_constraint_solver_add_edit_variable (solver, opposite_size, GTK_CONSTRAINT_WEIGHT_MEDIUM * 2.0);
+      gtk_constraint_solver_begin_edit (solver);
+      gtk_constraint_solver_suggest_value (solver, opposite_size, for_size);
+      gtk_constraint_solver_resolve (solver);
 
-  gtk_constraint_solver_suggest_value (solver, size, 0.0);
-  gtk_constraint_solver_suggest_value (solver, opposite_size, for_size >= 0 ? for_size : 0.0);
+      value = gtk_constraint_variable_get_value (size);
 
-  gtk_constraint_solver_resolve (solver);
+      gtk_constraint_solver_remove_edit_variable (solver, opposite_size);
+      gtk_constraint_solver_end_edit (solver);
+    }
+  else
+    {
+      value = gtk_constraint_variable_get_value (size);
+    }
 
   GTK_NOTE (LAYOUT,
             g_print ("layout %p preferred %s size: %.3f (for opposite size: %d)\n",
@@ -786,13 +783,6 @@ gtk_constraint_layout_measure (GtkLayoutManager *manager,
                      gtk_constraint_variable_get_value (size),
                      for_size));
 
-  value = gtk_constraint_variable_get_value (size);
-
-  gtk_constraint_solver_remove_edit_variable (solver, size);
-  gtk_constraint_solver_remove_edit_variable (solver, opposite_size);
-
-  gtk_constraint_solver_end_edit (solver);
-
   for (guint i = 0; i < size_constraints->len; i++)
     {
       GtkConstraintRef *ref = g_ptr_array_index (size_constraints, i);